<?php

namespace app\model;

use think\Model;
use think\model\relation\HasMany;

class PackageAttr extends Model
{
    public function product(): HasMany
    {
        return $this->hasMany(PackageProduct::class, "attr");
    }

    public function packDetail()
    {
        $id = request()->param("id");
        $data = $this->with(["product" => function ($query) {
            $query->with(["productData"]);
        }])->where([
            "id" => $id,
//            "is_del" => 1
        ])->find();
        if (empty($data)) throw new \Exception("产品异常");
        $data = $data->toArray();
        $data['checked'] = $data['radio'] = [];
        foreach ($data['product'] as $value) {
            if ($value['type'] == 1) $data['checked'][] = $value['product'];
            else $data['radio'][] = $value['product'];
        }
        return $data;
    }
}
